#ifndef __DATA_REPORT_H__
#define __DATA_REPORT_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <time.h>

#include "MQTTClient.h"
#include "cJSON.h"
#include "shmem.h"
#include "msg_queue_peer.h"
#include "sqlite3.h"

#define CLIENTID "data_report_client"
#define TOPIC_UP "/app/data/up"
#define TOPIC_DOWN "/app/data/down"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
#define FILE_NAME "/mnt/config/node.json"

union val_t {
    int b_val;   //bool类型存储空间
    int i_val;   //整形值存储空间
    float f_val; //浮点值存储空间
};

struct std_node
{
    int key;             //唯一键值
    int type;            //数据点类型
    int dev_type;        //数据点属于哪个设备，根据网关支持的设备自行定义
    union val_t old_val; //变化上报后需要更新旧值
    union val_t new_val; //从共享内存取出最新数据，放到new_val中
    int ret;             //默认为-1，采集成功后设置为0，采集失败再置-1
};

struct msgbuf
{
    long mtype;
    int key;
    char mdata[256];
};

struct database_t
{
    int key;
    char val[32];
    time_t timestamp;
};

void delivered(void *, MQTTClient_deliveryToken);
int msgarrvd(void *, char *, int, MQTTClient_message *);
void connlost(void *, char *);

void general_report(char *);

void *handler_report(void *);
void *handler_data(void *);

int all_report();

int pattern_alter(cJSON *, int, int);

char *select_history(int, time_t *);

int insert_data(int);

#endif